import * as Cesium from 'cesium';

class BDRegionCode {
  constructor(viewer) {
    this.defaultPos = [117.1573364319815, 39.74026123397825, 1000]
    this.viewer = viewer;
    this.currentPos = this.defaultPos
    this.latD = ''
    this.lonD = ''
    this.codeArr = []
    this.codeCfg = [
      {
        code: [['S', 'N']],
        cell: [180, 90],
        highPrecision: [1327104000, 663552000]
      },
      {
        code: [['0', '1', '2', '3', '4', '5', '6'],
        ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
        ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V']],
        cell: [6, 4],
        highPrecision: [44236800, 29491200]
      },
      {
        code: [['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B'], ['0', '1', '2', '3', '4', '5', '6', '7']],
        cell: [1 / 2, 1 / 2],
        highPrecision: [3686400, 3686400]
      },
      {
        code: [['0', '1', '2', '3', '4', '5']],
        strArr: ['0-0', '1-0', '0-1', '1-1', '0-2', '1-2'],
        cell: [1 / 4, 1 / 6],
        magnification: [15, 10, 60],
        power: [15, 10, 60],
        highPrecision: [1843200, 1228800]
      },
      {
        code: [['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E'],
        ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']],
        cell: [1 / 60, 1 / 60, 60],
        magnification: [1, 1, 60],
        highPrecision: [122880, 122880]
      },
      {
        code: [['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E'],
        ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E']],
        cell: [1 / 900, 1 / 900],
        magnification: [4, 4, 3600],
        highPrecision: [8192, 8192]
      },
      {
        code: [['0', '1', '2', '3']],
        cell: [1 / 1800, 1 / 1800],
        highPrecision: [4096, 4096]
      },
      {
        code: [['0', '1', '2', '3', '4', '5', '6', '7'], ['0', '1', '2', '3', '4', '5', '6', '7']],
        cell: [1 / 14400, 1 / 14400],
        highPrecision: [512, 512]
      },
      {
        code: [['0', '1', '2', '3', '4', '5', '6', '7'], ['0', '1', '2', '3', '4', '5', '6', '7']],
        cell: [1 / 115200, 1 / 115200],
        highPrecision: [64, 64]
      },
      {
        code: [['0', '1', '2', '3', '4', '5', '6', '7'], ['0', '1', '2', '3', '4', '5', '6', '7']],
        cell: [1 / 921600, 1 / 921600],
        highPrecision: [8, 8]
      },
      {
        code: [['0', '1', '2', '3', '4', '5', '6', '7'], ['0', '1', '2', '3', '4', '5', '6', '7']],
        cell: [1 / 7372800, 1 / 7372800],
        highPrecision: [1, 1]
      },
    ]
  }
  flyToDefaultPos () {
    this.viewer.camera.flyTo({
      destination: Cesium.Cartesian3.fromDegrees(...this.defaultPos),
      orientation: {
        heading: Cesium.Math.toRadians(0.0),
        pitch: Cesium.Math.toRadians(-90.0),
        roll: 0.0
      }
    });
  }
  getGridCodeBD (posArr = this.defaultPos) {
    this.currentPos = posArr
    this.codeCfg.forEach((element, index) => {
      this.getLevelCode(index, element)
    });
  }
  getLevelCode (index, element) {
    if (index === 0) {
      this.codeArr.push(this[`getCodeLevel${index}`](this.defaultPos, index));
    }
    if (index === 1) {
      this.codeArr.push(this[`getCodeLevel${index}`](this.defaultPos, index));
    }
    if (index === 2) {
      this.codeArr.push(this[`getCodeLevel${index}`](this.defaultPos, index));
    }
    if (index === 3) {
      this.codeArr.push(this[`getCodeLevel${index}`](this.defaultPos, index));
      console.log(this.codeArr);
    }
  }
  getCodeLevel0 (posArr, index) {
    const str = this.codeCfg[index].code[0]
    return { code: this.latD = posArr[1] < 0 ? str[0] : posArr[1] > 0 ? str[1] : '0', pos: [0, 0, 90, 180] }
  }
  getCodeLevel1 (posArr, index) {
    const xsiez = this.codeCfg[index].cell[0]
    const ysiez = this.codeCfg[index].cell[1]
    const c1 = Math.floor(Math.abs(posArr[0]) / xsiez) + 1
    const str1 = c1 < 10 ? '0' + c1 : c1
    const c2 = Math.floor(Math.abs(posArr[1]) / ysiez) + 1
    const str2 = String.fromCharCode(c2 + 64)
    const c3 = c1 * xsiez > posArr[0] ? (c1 - 1) * xsiez : (c1 + 1) * xsiez
    const c4 = c2 * ysiez > posArr[1] ? (c2 - 1) * ysiez : (c2 + 1) * ysiez
    return { code: `${str1}${str2}`, pos: [c1 * xsiez, c2 * ysiez, c3, c4].sort((a, b) => a - b) }
  }
  getCodeLevel2 (posArr, index) {
    // (待开发西北、东南、西南)
    this.lonD = posArr[0] > 0 ? 'E' : posArr[1] < 0 ? 'W' : '0'
    // 东北
    if (this.lonD === 'E' && this.latD === 'N') {
      const xmin = this.codeArr[index - 1].pos[2]
      const ymin = this.codeArr[index - 1].pos[0]
      const xsiez = this.codeCfg[index]['cell'][0]
      const ysiez = this.codeCfg[index]['cell'][1]
      const c1 = Math.floor((posArr[0] - xmin) / xsiez) + 1
      const str1 = this.codeCfg[index]['code'][0][c1 - 1]
      const c2 = Math.floor((posArr[1] - ymin) / ysiez) + 1
      const str2 = this.codeCfg[index]['code'][0][c2 - 1]
      const c3 = xmin + c1 * xsiez
      const c4 = ymin + c2 * ysiez
      return {
        code: `${str1}${str2}`,
        pos: [xmin + (c1 - 1) * xsiez, ymin + (c2 - 1) * ysiez, c3, c4].sort((a, b) => a - b)
      }
    }
    return { code: ``, pos: [] }
  }
  getCodeLevel3 (posArr, index) {
    console.log(posArr);
    console.log(this.codeCfg[index]);
    console.log(this.codeArr[index - 1].pos);
    const xmin = this.codeArr[index - 1].pos[2]
    const ymin = this.codeArr[index - 1].pos[0]
    const xsiez = this.codeCfg[index]['cell'][0]
    const ysiez = this.codeCfg[index]['cell'][1]
    const c1 = Math.floor((posArr[0] - xmin) / xsiez) + 1
    const c2 = Math.floor((posArr[1] - ymin) / ysiez) + 1
    const str = this.codeCfg[index]['strArr'].indexOf(`${c1 - 1}-${c2 - 1}`)
    return {
      code: `${str}`,
      posH: [
        xmin * 4 + (c1 - 1) * 1,
        ymin * 6 + (c2 - 1) * 1,
        xmin * 4 + c1 * 1,
        ymin * 6 + c2 * 1
      ].sort((a, b) => a - b),
      pos: [
        xmin + (c1 - 1) * xsiez,
        ymin + (c2 - 1) * ysiez,
        xmin + c1 * xsiez,
        ymin + c2 * ysiez,
      ].sort((a, b) => a - b)
    }
  }
}

export default BDRegionCode;
